//
//  RootViewController.m
//  TableViewExample
//

#import "RootViewController.h"

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];

	arrAsiaPacific = [[NSArray alloc] initWithObjects:
					  @"Australia",
					  @"Japan",
					  @"New Zealand",
					  @"South Korea", nil];
	arrEurope = [[NSArray alloc] initWithObjects:
				 @"Germany",
				 @"France",
				 @"Great Britain",
				 @"Italy", nil];
	arrNorthAmerica = [[NSArray alloc] initWithObjects:
					   @"Canada",
					   @"United States", nil];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(int)section {
	switch (section) {
		case 0:
			return @"Asia Pacific";
			break;
			
		case 1:
			return @"Europe";
			break;
			
		case 2:
			return @"North America";
			break;
			
		default:
			return nil;
			break;
	}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	switch (section) {
		case 0:
			return [arrAsiaPacific count];
			break;
			
		case 1:
			return [arrEurope count];
			break;

		case 2:
			return [arrNorthAmerica count];
			break;

		default:
			return 0;
			break;
	}
}

- (UITableViewCell *)tableView:(UITableView *)tableView
		 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Configure the cell.
	NSString *country;
	switch (indexPath.section) {
		case 0:
			country = [arrAsiaPacific objectAtIndex:indexPath.row];
			break;
		
		case 1:
			country = [arrEurope objectAtIndex:indexPath.row];
			break;
			
		case 2:
			country = [arrNorthAmerica objectAtIndex:indexPath.row];
			break;
	}
	NSString *imageName = [NSString stringWithFormat:@"%@.png", country];
	cell.imageView.image = [UIImage imageNamed:imageName];
	cell.textLabel.text = country;

    return cell;
}

- (void)dealloc {
	[arrAsiaPacific release];
	[arrEurope release];
	[arrNorthAmerica release];
    [super dealloc];
}

@end
